package com.tms.app.tms.receiver;

import android.app.usage.UsageEvents;
import android.content.Context;
import android.content.Intent;

import com.alibaba.sdk.android.push.MessageReceiver;
import com.alibaba.sdk.android.push.notification.CPushMessage;
import com.tms.app.tms.bean.Msg;
import com.tms.app.tms.bean.NotionBean;
import com.tms.app.tms.db.MsgDBUtil;
import com.tms.app.tms.db.TaskInfoDBUtil;
import com.tms.app.tms.ui.home.MainActivity;
import com.tms.app.tms.ui.login.LoginAt;
import com.tms.app.tms.utils.SharePreferenceUtil;
import com.tms.app.tms.utils.TimeUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.Date;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/5 0005.
 */

public class MyMsgReceiver extends MessageReceiver {
    @Override
    protected void onMessage(Context context, CPushMessage cPushMessage) {
        super.onMessage(context, cPushMessage);
    }

    /*****
     * 通知打开时调用
     * @param context
     * @param s
     * @param s1
     * @param s2
     */
    @Override
    protected void onNotificationOpened(Context context, String s, String s1, String s2) {
        super.onNotificationOpened(context, s, s1, s2);
        Intent mIntent=new Intent(context,MainActivity.class);
        mIntent.putExtra("showNav",1);
      //  mIntent.putExtras(bundle);
      //  mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(mIntent);
    }

    /***
     * 通知到达时调用
     * @param context
     * @param s
     * @param s1
     * @param map
     */
    @Override
    protected void onNotification(Context context, String s, String s1, Map<String, String> map) {
        super.onNotification(context, s, s1, map);

        int type=Integer.parseInt(map.get("notice_category"));
        int planId=Integer.parseInt(map.get("planId"));
        NotionBean notionBean=new NotionBean(type);//"notice_category" -> "0" 任务   ,1 变更
        if (type==0){
            int onNotification=SharePreferenceUtil.getInt(context,"tzhi",0);
            SharePreferenceUtil.setValue(context,"tzhi",onNotification+1);
        }else if (type==1){
            int onNotification=SharePreferenceUtil.getInt(context,"tzhi-BG",0);// 系统通知
            SharePreferenceUtil.setValue(context,"tzhi-BG",onNotification+1);
        }else {
            int onNotification=SharePreferenceUtil.getInt(context,"tzhi-XT",0);// 系统通知
            SharePreferenceUtil.setValue(context,"tzhi-XT",onNotification+1);
        }

        try {
            int allotPlanId= Integer.parseInt(map.get("planId"));
            String allotPlanNo=map.get("planNo");
            if (type==1){
                Msg msg=new Msg();
                msg.setMsgType(2); //0任务消息,1系统消息,2 变更通知
                msg.setAllotPlanNo(allotPlanNo);
                msg.setPlanId(allotPlanId);
                msg.setContent(s1);
                msg.setTitle(s);
                msg.setPlanId(planId);

                try {
                    msg.setPublishTime(new Date().getTime());
                } catch (NumberFormatException e) {
                    e.printStackTrace();

                }
                if (null==map.get("msgId")||map.get("msgId").equals(""))
                    msg.setMsgId(34);
                else
                    msg.setMsgId(Integer.parseInt(map.get("msgId")));
                msg.setPublishTime(new Date().getTime());
                MsgDBUtil.saveOrUpadateMsg(msg,2);
                TaskInfoDBUtil.updateTaskReadType(allotPlanId,0);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
        EventBus.getDefault().post(notionBean);

    }

    @Override
    protected void onNotificationReceivedInApp(Context context, String s, String s1, Map<String, String> map, int i, String s2, String s3) {
        super.onNotificationReceivedInApp(context, s, s1, map, i, s2, s3);
    }
}
